package com.cw1.principle.openClose;

/**
 * 开闭合原则方式一：利用基类进行继承来实现判断
 */
public class Ocp {
    public static void main(String[] args) {
        // 存在问题:扩展性极差,例如加入一个画出三角形
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());

        //
    }
}

// 这是一个绘图的类
class GraphicEditor{
    // 接收Shape对象，然后根据type，来绘制不同的图形
    void drawShape(Shape shape){
        if (shape.type == 1){
            drawRectangle(shape);
        }else if (shape.type == 2){
            drawCircle(shape);
        }else if (shape.type == 3){
            drawTriangle(shape);
        }
    }

    void drawRectangle(Shape shape){
        System.out.println("画出矩形");
    }

    void drawCircle(Shape shape){
        System.out.println("画出圆形");
    }

    void drawTriangle(Shape shape){
        System.out.println("画出三角形");
    }
}

// Shape类,基类
class Shape{
    int type;
}
// 矩形类
class Rectangle extends Shape{
    Rectangle(){
        super.type = 1;
    }
}
// 圆形类
class Circle extends Shape{
    Circle(){
        super.type = 2;
    }
}
// 三角形类
class Triangle extends Shape{
    Triangle(){
        super.type = 3;
    }
}


